<?php
/**
 * Model for performing CRUD operations on roles database table.
 * @package backend-roles/models
 * @category Model
 * 
 * @author Mark Angelo Angulo
 * @copyright (c) 2015
 */
class Questionnaires_model extends Crud {
    
    /**
     *  Constructor method.
     */
    function __construct()
    {
	parent::__construct();

        $this->table            = 'questionnaires';
        $this->primary_key      = 'QuestionnaireID';
    }
    
    /**
     * Retrive a certain record. Create it if it doesn't exist.
     * @access public
     * @param array $args An array containing filters to the database query.
     * @param array $record An array containing the information about the record.
     * @return array Returns the record.
     */
    public function push($args = array(), $sport_id = NULL, $type_id = NULL, $category_id = NULL, $record = array())
    {
        $questionnaire_id = NULL;
        
        $this->db->where($args);
        $questionnaires = $this->db->get($this->table)->result_array();
        $this->db->flush_cache();
        
        if ( empty($questionnaires) ) // if it doesn't exist, we create it.
        {
            $questionnaire_id = $this->insert($record);
        } else
        {
            $existing = FALSE;
            
            foreach( $questionnaires as $questionnaire )
            {
                $questionnaire_id = $questionnaire[$this->primary_key];

                // check if same sport
                $this->db->where( array('QuestionnaireID' => $questionnaire_id, 'SportID' => $sport_id) );
                $questionnaire_sport_id = $this->db->get('questionnaire_sports')->row_array();
                $this->db->flush_cache();

                // check if same category
                $this->db->where( array('QuestionnaireID' => $questionnaire_id, 'QRCategoryID' => $category_id) );
                $questionnaire_category_id = $this->db->get('questionnaire_categories')->row_array();
                $this->db->flush_cache();

                // check if same type
                $this->db->where( array('QuestionnaireID' => $questionnaire_id, 'QRTypeID' => $type_id) );
                $questionnaire_type_id = $this->db->get('questionnaire_types')->row_array();
                $this->db->flush_cache();

                // if not same sport, category or type create the record again
                if( ! ( ( empty($questionnaire_sport_id) && $sport_id != NULL ) || 
                    ( empty($questionnaire_category_id) && $category_id != NULL ) ||
                    ( empty($questionnaire_type_id) && $type_id != NULL ) ) )
                {
                    $existing = TRUE;
                    break;
                }
            }
            
            if( ! $existing )
            {
                $questionnaire_id = $this->insert($record);
            }
            
        }
        
        return $questionnaire_id;
    }
}

/* End of file role_model.php */
/* Location: ./application/roles/backend-roles/models/role_model.php */